package com.example.demo.controller.order;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import com.example.demo.common.IsObjectEmpty;
import com.example.demo.domain.AjaxResult;
import com.example.demo.domain.order.SaleOrder;
import com.example.demo.domain.order.SaleOrderItem;
import com.example.demo.domain.service.Service;
import com.example.demo.domain.tos.Tos;
import com.example.demo.domain.tovip.Tovip;
import com.example.demo.service.order.SaleOrderItemService;
import com.example.demo.service.service.ServiceService;
import com.example.demo.service.tos.TosService;
import com.example.demo.service.tovip.TovipService;
import com.example.demo.web.TableDataInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.demo.controller.BaseController;
import com.example.demo.domain.userInfo.UserInfo;
import com.example.demo.domain.vip.Vip;
import com.example.demo.service.order.SaleOrderService;
import com.example.demo.service.vip.VipService;


@Controller
@RequestMapping("/saleOrderItem")
public class SaleOrderItemController extends BaseController {
    @Autowired
    private VipService vipService;

    @Autowired
    private SaleOrderService saleOrderService;

    @Autowired
    private SaleOrderItemService saleOrderItemService;

    @Autowired
    private TosService tosService;

    @Autowired
    private ServiceService serviceService;

    @Autowired
    private TovipService tovipService;

    //加载管理的页面
    @GetMapping("")
    public String list(ModelMap modelMap) {
        List<Tos> tosList = tosService.getTosTree();
        modelMap.put("Tos", tosList);
        return "cashier/newOrder";
    }

    @GetMapping("/orderList")
    public String orderList() {
        return "cashier/OrderManage";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SaleOrder saleOrder) {
        startPage();
        //只查看未支付以及未退掉的订单
        saleOrder.setPayState(1);
        saleOrder.setDelFlag(1);
        List<SaleOrder> list = saleOrderService.selectSaleOrderList(saleOrder);
        return getDataTable(list);
    }


    @PostMapping("/edit")
    @ResponseBody
    public  AjaxResult edit(SaleOrder saleOrder, Integer[] serviceId){

        for(Integer i : serviceId){
            System.out.println(i);
        }
        return toAjax(saleOrderItemService.updateByPrimaryKeySelective(saleOrder,serviceId));
    }


    //	//查询方法，并将结果渲染到表格中
    @PostMapping("/searchVip")
    @ResponseBody
    public Vip list(Integer tel) {
        Vip vip = new Vip();
        vip.setVipId(tel);
        Vip list = vipService.selectVip(vip);
        return list;
    }

    @PostMapping("/selectBname")
    @ResponseBody
    public List<UserInfo> selectBarberName() {
        // TODO Auto-generated method stub
        return this.saleOrderService.selectBarberName();
    }

    @PostMapping("/calculatePrice")
    @ResponseBody
    public BigDecimal[] calculatePrice(Integer[] servicesIds, Integer vipId) {
        BigDecimal[] result = saleOrderItemService.calculatePrice(servicesIds,vipId);
        return result;
    }

    //添加订单
    @PostMapping("/addOrder")
    @ResponseBody
    public AjaxResult addOrder(SaleOrder saleOrder, Integer[] serviceId) {

        //设置时间
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        saleOrder.setCreateTime(timestamp);
        String time = timestamp.toString();
        String str = time.replaceAll("[[\\s-:punct.:]]", "");
        saleOrder.setSaleorderno(str);
        saleOrder.setBarberId(saleOrder.getBarberId());
        //1为未支付状态
        saleOrder.setPayState(1);
        //获取收银员
        UserInfo user = (UserInfo) SecurityUtils.getSubject().getPrincipal();
        if (IsObjectEmpty.checkObjAllFieldsIsNull(user)) {
            SecurityUtils.getSubject().logout();
            return error("用户身份无法读取，请重新登录！");
        }
        saleOrder.setCashierId(user.getUid());

        //插入订单中
        return toAjax(saleOrderItemService.insert(saleOrder,serviceId));
    }

    //查看订单明细
    @GetMapping("/orderItem")
    public String orderItem(Integer orderId,ModelMap modelMap) {
        SaleOrder saleOrder =saleOrderService.getSaleOrderItem(orderId);
        List<Tos> tosList = tosService.getTosTree();
        modelMap.put("Tos", tosList);
        modelMap.put("saleOrder",saleOrder);
        return "cashier/OrderEdit";
    }


}
